package com.order.controller.seller;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.order.pojo.ProductCategory;
import com.order.pojo.ProductInfo;
import com.order.service.IOrderMasterService;
import com.order.service.ProductInfoService;
import com.order.service.PushMessageService;
import com.order.utils.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author llr
 * @since 2025-08-11
 */
@Api(tags = "餐饮接口")
@RestController
@ApiResponses({
        @ApiResponse(code = 200, message = "新增成功", response = ProductCategory.class),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 403, message = "权限被禁止"),
        @ApiResponse(code = 500, message = "服务器内部错误")
})
@RequestMapping("/seller/productInfo")
public class ProductInfoController {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    IOrderMasterService orderMasterService;
    @Autowired
    PushMessageService pushMessageService;
  @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/list")
    public Result findAll() {
        return Result.success(productInfoService.list());
    }

    @GetMapping("/selectPage")
    public Result selectPage(ProductInfo productInfo,
                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        PageInfo<ProductInfo> pageInfo = productInfoService.selectpage(pageNum, pageSize, productInfo);
        return Result.success(pageInfo);
    }

    @ApiOperation("根据ID查询餐饮")
    @GetMapping("/list/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "列表ID"),
    })
    public Result getById(@PathVariable("productId") String productId) {
        return Result.success(productInfoService.getById(productId));
    }

    @ApiOperation("新增餐饮")
    @PostMapping("/save")
    public Result save(@RequestBody ProductInfo productInfo) {
        String id = String.valueOf(IdUtil.getSnowflake().nextId());
        productInfo.setProductId(id);
        redisTemplate.delete("cacheKey");
        return Result.success(productInfoService.save(productInfo));
    }

    @ApiOperation("修改餐饮")
    @PutMapping("/update")
    public Result update(@RequestBody ProductInfo productInfo) {
        redisTemplate.delete("cacheKey");
        return Result.success(productInfoService.updateById(productInfo));
    }

    @ApiOperation("删除餐饮")
    @DeleteMapping("delete/{productId}")
    public Result delete(@PathVariable("productId") String productId) {
        redisTemplate.delete("cacheKey");
        ProductInfo productInfo = new ProductInfo(productId, 1);

        return Result.success(productInfoService.updateById(productInfo));
    }

    @ApiOperation("取消订单")
    @DeleteMapping("delete2/{orderId}")
    public Result delete2(@PathVariable("orderId") String orderId) {
        orderMasterService.updateByPd(orderId);


        return Result.success();
    }
}
